package com.techsun.datanow.sync

import com.microsoft.azure.cosmosdb.spark.config.Config
import com.microsoft.azure.cosmosdb.spark.schema._
import org.apache.kudu.spark.kudu.KuduContext
import org.apache.log4j.{Level, Logger}
import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.functions._
import org.apache.spark.sql.types._


class Sync_BehaviorRecord extends TDataSync {
  /**
   * 同步工作方法
   *
   * @param spark             spark 会话对象
   * @param cosmosDbEndpoint  CosmosDB Endpoint
   * @param cosmosDbMasterkey CosmosDB Endpoint 对应的密钥
   * @param kuduMaster        kudu Master 节点
   * @return 同步成功或失败
   */
  override def sync(spark: SparkSession, cosmosDbEndpoint: String, cosmosDbMasterkey: String, kuduMaster: String, beginTime: Long, endTime: Long): Boolean = {
    import spark.implicits._
    try {
      Logger.getLogger("org").setLevel(Level.ERROR)
      Logger.getLogger("com").setLevel(Level.ERROR)
      val tableName = "sh_tsrbehaviorrecord"
      val kuduContext = new KuduContext(kuduMaster, spark.sparkContext)
      val config = Config(Map(
        "Endpoint" -> cosmosDbEndpoint,
        "Masterkey" -> cosmosDbMasterkey,
        "Database" -> "VF_Vans_CN",
        "preferredRegions" -> "China East 2",
        "Collection" -> "TsrBehaviorRecord",
        "query_custom" ->
          s"""
             |select
             |c.id,
             |c.customerId customerid,
             |c.memberId memberid,
             |c.source,
             |c.type,
             |c.name,
             |c.occurTime occurtime,
             |c.storeId storeid,
             |c.birthday,
             |c.awardType awardtype,
             |c.tierId tierid,
             |c.tileId tileid,
             |c.skuId skuid,
             |c.getWay getway,
             |c.expressAddress expressaddress,
             |c.expressMobile expressmobile,
             |c.expressName expressname,
             |c.storeCode storecode,
             |c.storeName storename,
             |c.pointRecordId pointrecordid,
             |c.createTime createtime,
             |c.updateTime updatetime,
             |c.status,
             |c._ts
             |from c
             |where c._ts > ${beginTime}
             |""".stripMargin
      ))

      val schema = StructType(
        List(StructField("id", StringType, true),
          StructField("customerid", StringType, true),
          StructField("memberid", StringType, true),
          StructField("source", IntegerType, true),
          StructField("type", IntegerType, true),
          StructField("name", StringType, true),
          StructField("occurtime", TimestampType, true),
          StructField("storeid", StringType, true),
          StructField("birthday", TimestampType, true),
          StructField("awardtype", IntegerType, true),
          StructField("tierid", StringType, true),
          StructField("tileid", StringType, true),
          StructField("skuid", StringType, true),
          StructField("getway", IntegerType, true),
          StructField("expressaddress", StringType, true),
          StructField("expressmobile", StringType, true),
          StructField("expressname", StringType, true),
          StructField("storecode", StringType, true),
          StructField("storename", StringType, true),
          StructField("pointrecordid", StringType, true),
          StructField("createtime", TimestampType, true),
          StructField("updatetime", TimestampType, true),
          StructField("status", IntegerType, true),
          StructField("_ts", IntegerType, true)
        ))
      val df = spark.sqlContext.read.schema(schema).cosmosDB(config)
      val count = df.count();
      printf("Sync_TsrBehaviorRecord query count: %d\n", count)
      kuduContext.upsertRows(df, tableName)
      return true
    } catch {
      case ex: Exception => {
        println("Sync_TsrBehaviorRecord exception: " + ex.getMessage)
        return false
      }
    }
  }
}
